package cn.com.shbs.Service.ShopInfoService;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shbs.Dao.ShopInfoDao.BaseRoleInfoDao;
import cn.com.shbs.Dao.ShopInfoDao.ShopInfoDao;
import cn.com.shbs.Dao.ShopInfoDao.StoreRoleInfoDao;
import cn.com.shbs.Dao.UserInfoDao.UserInfoDao;
import cn.com.shbs.Service.ShopInfoService.IShopInfoService;
import cn.com.shbs.Service.UserInfoService.IUserInfoService;
import cn.com.shbs.Service.UserInfoService.UserInfoServiceImpl;
import cn.com.shbs.model.BaseRole;
import cn.com.shbs.model.LoginUser;
import cn.com.shbs.model.Store;
import cn.com.shbs.model.StoreRole;;

@Service
public class ShopInfoServiceImpl implements IShopInfoService {

	static Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
	@Autowired
	public ShopInfoDao shopInfoDao;// 店面信息表

	@Autowired
	public StoreRoleInfoDao storeroleInfoDao; // 店面角色

	@Autowired
	public BaseRoleInfoDao baseroleInfoDao; // 角色基本表

	@Autowired
	IUserInfoService userinfoservice;

	@Override
	public void addshopinfo(Store store) throws Exception {
		// TODO Auto-generated method stub

		logger.info("ShopInfoServiceImpl.addshopinfo() 方法 start");

		try {
			// 添加店铺信息
			shopInfoDao.addshopinfo(store);
			// 添加用户表

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.addshopinfo()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.addshopinfo() 方法 end");

	}

	@Override
	@Transactional(rollbackFor = Exception.class) // 回滚数据
	public void addstroeinfos(Store store, LoginUser userInfo) throws Exception {
		// TODO Auto-generated method stub

		logger.info("ShopInfoServiceImpl.addshopinfo() 方法 start");

		try {
			// 添加店铺信息
			shopInfoDao.addshopinfo(store);
			
			//设置用户表和店铺信息表关系
			userInfo.setBelong_store(store.getStore_code());//设置店铺ID
			//userInfo.setStore_role_code(store_role_code);//设置角色ID
			// 添加用户表
			userinfoservice.Modfiysysuser(userInfo);

			// 查询角色基本表
			List<BaseRole> baseRoleslist = new ArrayList<BaseRole>();
			baseRoleslist = baseroleInfoDao.findbaseRoles();
			
			if (baseRoleslist.size() > 0) {

				BaseRole baseRole = null;
				for (int i = 0; i < baseRoleslist.size(); i++) {
					baseRole = new BaseRole();
					baseRole.setRole_code(baseRoleslist.get(i).getRole_code());
					baseRole.setRole_name(baseRoleslist.get(i).getRole_name());
					baseRole.setRoyalty_rate(baseRoleslist.get(i).getRoyalty_rate());

					// 添加店面角色表
					StoreRole stoerRole = new StoreRole();
					stoerRole.setStore_code(store.getStore_code());
					stoerRole.setRole_name(baseRole.getRole_name());
					stoerRole.setRole_code(baseRole.getRole_code());
					stoerRole.setRoyalty_rate(baseRole.getRoyalty_rate());

					storeroleInfoDao.add_storerole(stoerRole);
				}

			}
			

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.addshopinfo()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.addshopinfo() 方法 end");

	}

	@Override
	public BaseRole findBaseRoleInfos() throws Exception {
		// TODO Auto-generated method stub
		logger.info("ShopInfoServiceImpl.findBaseRoleInfos() 方法 start");

		BaseRole baseRole = new BaseRole();
		try {

			baseRole = baseroleInfoDao.findBaseRoleInfos();

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.findBaseRoleInfos()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.findBaseRoleInfos() 方法 end");

		return baseRole;

	}

	@Override
	public List<BaseRole> findbaseRoles() throws Exception {
		// TODO Auto-generated method stub

		List<BaseRole> baseRoleList = null;

		try {

			baseRoleList = baseroleInfoDao.findbaseRoles();

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.findBaseRoleInfos()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.findBaseRoleInfos() 方法 end");

		return baseRoleList;

	}

	@Override
	public void add_storerole(StoreRole stoerRole) throws Exception {
		// TODO Auto-generated method stub
		logger.info("ShopInfoServiceImpl.add_storerole() 方法 start");

		try {

			// 添加店面角色表
			storeroleInfoDao.add_storerole(stoerRole);

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.add_storerole()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.add_storerole() 方法 end");

	}

	@Override
	public void UpdateShop(Store store) throws Exception {
		// TODO Auto-generated method stub

		try {

			// 更新店面信息
			shopInfoDao.UpdateShop(store);

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.UpdateShop()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.UpdateShop() 方法 end");
	}

	@Override
	public List<Store> FindShopByName(HashMap map) throws Exception {
		// TODO Auto-generated method stub
		
		List<Store> store_list=null;
		
		try {

			store_list=shopInfoDao.FindShopByName(map);
			

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.FindShopByName()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.FindShopByName() 方法 end");
		
		return store_list;
	}

	@Override
	public int FindShopByNameNums(Store store) throws Exception {
		// TODO Auto-generated method stub
		
		int nums=0;
		
		try {

			nums=shopInfoDao.FindShopByNameNums(store);

		} catch (Exception e) {
			logger.error("ShopInfoServiceImpl.FindShopByNameNums()", e);
			throw e;
		}

		logger.info("ShopInfoServiceImpl.FindShopByNameNums() 方法 end");
		
		return nums;
		
		
	}

	

}
